﻿Imports FinanceiroDAL
Imports FinanceiroBLL
Imports System.Text.RegularExpressions
Public Class CadastrodeBancosFRM
    Dim objBLL As New BancosBLL
    Dim operacao As String
    Public Op As String
    Public Codigo As Integer

    Public Sub New()
        InitializeComponent()
    End Sub
#Region "Carregar Campos"
    Sub carrega_campos(ByVal codigo As Integer)
        objBLL = New BancosBLL(codigo)
        TCodigo.Text = objBLL.pbanCodigo
        TNome.Text = objBLL.pbanNome
        TEndereco.Text = objBLL.pbanEndereco.ToString
        TNum.Text = objBLL.pbanEndNum.ToString
        TBairro.Text = objBLL.pbanBairro.ToString
        MTBCep.Text = objBLL.pbanCEP.ToString
        TCidade.Text = objBLL.pbanCidade
        If (objBLL.pbanUf <> Nothing) Then
            CBUf.SelectedValue = objBLL.pbanUf
        End If
        TAgencia.Text = objBLL.pbanAgencia
        TContato.Text = objBLL.pbanContato
        THomePage.Text = objBLL.pbanHomePage
        If (objBLL.pbanTipo = "Bancária") Then
            CBTipo.SelectedIndex = 0
        Else
            CBTipo.SelectedIndex = 1
        End If
        MTBTel1.Text = objBLL.pbanTelefone1.ToString
        MTBTel2.Text = objBLL.pbanTelefone2.ToString
        MTBCelular.Text = objBLL.pbanCelular
        CBStatus.SelectedValue = objBLL.pstatus
    End Sub
#End Region
#Region "Carrega Propriedades"
    Sub CarregaPropriedades()
        objBLL.limpar()
        If (TCodigo.Text <> "") Then
            objBLL.pbanCodigo = TCodigo.Text
        Else
            objBLL.pbanCodigo = 0
        End If

        objBLL.pbanNome = Trim(TNome.Text)
        objBLL.pbanEndereco = Trim(TEndereco.Text)
        objBLL.pbanBairro = Trim(TBairro.Text)
        objBLL.pbanCidade = Trim(TCidade.Text)
        '  objBLL.pbanAgencia = Val(Trim(TAgencia.Text))
        If (TAgencia.Text <> Nothing) Then
            objBLL.pbanAgencia = Val(Trim(TAgencia.Text))
        Else
            objBLL.pbanAgencia = 0
        End If

        If (Replace(Replace(Replace(Replace(MTBTel1.Text, "(", ""), ")", ""), " ", ""), "-", "").Trim <> Nothing) Then
            objBLL.pbanTelefone1 = Val(Replace(Replace(Replace(Replace(MTBTel1.Text, "(", ""), ")", ""), " ", ""), "-", ""))
        Else
            objBLL.pbanTelefone1 = 0
        End If
        If (Replace(Replace(Replace(Replace(MTBTel2.Text, "(", ""), ")", ""), " ", ""), "-", "").Trim <> Nothing) Then
            objBLL.pbanTelefone2 = Val(Replace(Replace(Replace(Replace(MTBTel2.Text, "(", ""), ")", ""), " ", ""), "-", ""))
        Else
            objBLL.pbanTelefone2 = 0
        End If
        If (Replace(Replace(Replace(Replace(MTBCelular.Text, "(", ""), ")", ""), " ", ""), "-", "").Trim <> Nothing) Then
            objBLL.pbanCelular = Val(Replace(Replace(Replace(Replace(MTBCelular.Text, "(", ""), ")", ""), " ", ""), "-", ""))
        Else
            objBLL.pbanCelular = 0
        End If
        If (Replace(MTBCep.Text, "-", "").Trim <> Nothing) Then
            objBLL.pbanCEP = Val(Replace(MTBCep.Text, "-", ""))
            ' objBLL.pbanCEP = Replace(MTBCep.Text, "-", "")
        Else
            objBLL.pbanCEP = 0
        End If
        objBLL.pbanUf = CBUf.SelectedValue
        objBLL.pbanContato = Trim(TContato.Text)
        objBLL.pbanHomePage = Trim(THomePage.Text)
        If (CBTipo.SelectedItem = "Bancária") Then
            objBLL.pbanTipo = "Bancária"
        Else
            objBLL.pbanTipo = "Não Bancária"
        End If
        objBLL.pbanEndNum = Trim(TNum.Text)
        objBLL.pstatus = CBStatus.SelectedValue
    End Sub
#End Region
#Region "Limpar Campos"
    Sub limpar()
        TCodigo.Text = Nothing
        TNome.Text = Nothing
        TEndereco.Text = Nothing
        TBairro.Text = Nothing
        TCidade.Text = Nothing
        TAgencia.Text = Nothing
        MTBTel1.Text = Nothing
        MTBTel2.Text = Nothing
        MTBCelular.Text = Nothing
        MTBCep.Text = Nothing
        CBUf.SelectedItem = ""
        TContato.Text = Nothing
        THomePage.Text = Nothing
        CBTipo.SelectedItem = ""
        TNum.Text = Nothing
        ' CBStatus.SelectedItem = ""

    End Sub
#End Region
#Region "Validação"

    Sub AtivaReadyOnly()
        TEndereco.ReadOnly = True
        TNum.ReadOnly = True
        TBairro.ReadOnly = True
        MTBCep.ReadOnly = True
        TCidade.ReadOnly = True
        TAgencia.ReadOnly = True
        TContato.ReadOnly = True
        THomePage.ReadOnly = True
        MTBTel1.ReadOnly = True
        MTBTel2.ReadOnly = True
        MTBCelular.ReadOnly = True
    End Sub
    Sub DesativaReadyOnly()
        TEndereco.ReadOnly = False
        TNum.ReadOnly = False
        TBairro.ReadOnly = False
        MTBCep.ReadOnly = False
        TCidade.ReadOnly = False
        TAgencia.ReadOnly = False
        TContato.ReadOnly = False
        THomePage.ReadOnly = False
        MTBTel1.ReadOnly = False
        MTBTel2.ReadOnly = False
        MTBCelular.ReadOnly = False
    End Sub

#End Region
#Region "Salvar"
    Private Sub TSBSalvar_Click(sender As System.Object, e As System.EventArgs) Handles TSBSalvar.Click
        Try
            CarregaPropriedades()
            If Op = "I" Then
                objBLL.inserir()
                MessageBox.Show("Banco inserido com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                limpar()
                Me.Close()
            Else
                If (objBLL.Alterar()) Then
                    MessageBox.Show("Banco alterada com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    limpar()
                    Me.Close()
                Else
                    MsgBox("Banco não pode ser alterado. Verifique relacionamento com Contas", MsgBoxStyle.Exclamation)
                    limpar()
                    Me.Close()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region
#Region "Load"
    Private Sub CadastrodeBancosFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        objconexao.carregaCombo(CBUf, "Generico.Gen_Estado", "est_uf", "est_uf", "", "")
        objconexao.carregaCombo(CBStatus, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo=1 or sta_codigo=2", "sta_descricao")
        CBStatus.SelectedValue = 1
        CBTipo.SelectedValue = 1
        limpar()
        If (Op = "I") Then
            LBTitulo.Text = "Cadastro de Bancos"
            TCodigo.Text = ""
        Else
            carrega_campos(Codigo)
        End If
    End Sub
#End Region
#Region "Fechar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region
#Region "KeyPress"
    Private Sub TCidade_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TCidade.KeyPress
        If Not Char.IsLetter(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," And e.KeyChar = "" Then
            e.Handled = True
        End If
    End Sub

    Private Sub TAgencia_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TAgencia.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = "-" And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub

    Private Sub TNum_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TNum.KeyPress
        If Not Char.IsLetterOrDigit(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub

    Private Sub TNome_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TNome.KeyPress
        If Not Char.IsLetter(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," And Not e.KeyChar = vbBack And e.KeyChar = "" Then
            e.Handled = True
        End If
    End Sub

    Private Sub TEndereco_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TEndereco.KeyPress
        If Not Char.IsLetterOrDigit(e.KeyChar) And Not e.KeyChar = vbBack And e.KeyChar = "" Then
            e.Handled = True
        End If
    End Sub

    Private Sub TBairro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TBairro.KeyPress
        If Not Char.IsLetterOrDigit(e.KeyChar) And Not e.KeyChar = vbBack And e.KeyChar = "" Then
            e.Handled = True
        End If
    End Sub

    Private Sub MTBTel1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MTBTel1.KeyPress
        If Not Char.IsNumber(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub

    Private Sub MTBTel2_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MTBTel2.KeyPress
        If Not Char.IsNumber(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub

    Private Sub MTBCelular_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MTBCelular.KeyPress
        If Not Char.IsNumber(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub
#End Region
End Class